#!/usr/bin/env python

"""Nagios plugin for CPU time monitoring. Focusing on %iowait.
OS: Ubuntu, CentOS (requires sysstat package)
Python version: 2.6 +
"""
import sys
import os.path
import subprocess

IOSTAT = '/usr/bin/iostat'
IOSTAT_CMD = "IOSTAT -y 5 1"
TMP = '/tmp/nrpeiostatresult'
# Device we're interested in
DEV = ['hda', 'xvda', 'xvdb', 'sda', 'sdb', 'sdc', 'sdd', 'vda', 'vdb', 'vdc', 'vdd']

warning = {'iowait': 10.0}
critical = {'iowait': 20.0}


class Output:
    code = {'UNKNOWN': 3, 'CRITICAL': 2, 'WARNING': 1, 'OK': 0}
    def __init__(self):
        self.status = 'UNKNOWN'
        self.line = ' '


    def __repr__(self):
        return self.status + " %CPU" + self.line


def check(output):
    f = open(TMP, 'w')
    res = subprocess.check_call([IOSTAT, '-k', '-y', '5', '1'], stdout=f)
    f.close()
    f = open(TMP, 'r')
    stat = f.read()
    data = stat.split('\n')
    # CPU: %user, %nice, $system, %iowait, %steal, $idle
    u, nice, s, iow, stl, idl = data[3].split()
    output.status = "OK"
    output.line += "usr %s sys %s iowait %s idle %s |cpu_idle=%s;0;0;0;0" % (u, s, iow, idl, idl)

    # Devices:
    devs = data[5:-2]
#   output.line += " "+",".join(devs[0].split())  # column headers
    for line in devs:
       dev = line.split()
       if dev[0] in DEV:
            kB_read,kB_wrtn = dev[2:4]                                   #kB_read/s,kB_wrtn/s
            output.line += " " + "%s_read_kB=%s;0;0;0;0 %s_write_kB=%s;0;0;0;0" % (dev[0],kB_read,dev[0],kB_wrtn) # device row
#   iow
    output.line += " " + "iowait=%s;0;0;0;0" % (iow)
    if warning['iowait'] < float(iow):
        output.code = 1
        output.status = "WARNING"
    if critical['iowait'] < float(iow):
        output.code = 2
        output.status = "CRITICAL"
    #print iow, warning['iowait'] > iow, warning['iowait']
    return output

if __name__ == '__main__':
    status = Output()
    if not os.path.exists(IOSTAT):
        status.status = 'UNKNOWN'
        status.line = " iostat not found (install sysstat package)"
    else:
        status = check(status)
    print status
    sys.exit(status.code[status.status])
